<?php
class EquipmentAction extends Action{

	private function getAllequipment(){//【获取所有可使用的设备信息】
		$equipment = M('Equipment');
		$result = $equipment->where("status = 1")->select();
		return $result;
	}
	
	private function getUserdata($uid,$id){
		$equipment_user = M('Equipment_user');
		$data = $equipment_user->where("uid = '$uid' and id = '$id'")->find();
		//dump($data);exit;
		if($data){
			$var = json_decode($data['data'],true);
			if($var == null){$data['data'] = array();}else{$data['data'] = $var;}
			return $data;
		}else{

			return false;
		}
	}
	
	public function addEquipment(){//【用户添加设备】
		$data['uid'] = $uid = $_POST['uid'];
		$data['eid'] = $eid = $_POST['eid'];
		$equipment_user = M('Equipment_user');
//		$find = $equipment_user->where("uid = '$uid' and eid = '$eid'")->find();
		$equipment = M('Equipment');
		$status = $equipment->where("eid = '$eid' and status = '1'")->find();
//		if($find){//已添加过
//			$value = ['code'=>'added'];
//			header("Content-Type: text/json");
//			die(json_encode($value));
//		}
		if(!$status){//设备已关闭
			$value = ['code'=>'disable'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		$add = $equipment_user->data($data)->add();
		if($add){//成功
			$value = ['code'=>'success'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{//失败
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	public function deleteEquipment(){//【删除设备】
		$uid = $_POST['uid'];
		$id = $_POST['id'];
		$equipment_user = M('Equipment_user');
		$result = $equipment_user->where("id = '$id' and uid = '$uid'")->delete();
		if($result){
			$value = ['code'=>'success'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	
	}
	
	public function userEquipment(){//【获取用户所有设备和未使用的设备】
		$data['uid'] = $uid = $_POST['uid'];
		$equipment = M('Equipment');
		$equipment_user = M('Equipment_user');
		$find = $equipment_user->where("uid = '$uid' and eid = '2'")->find();
		if(!$find){
			$equipment_user->uid = $uid;
			$equipment_user->eid = 2;
			$equipment_user->add();
		}
		$data1  =$equipment->join("yjequipment_user U on yjequipment.eid = U.eid")->where("yjequipment.status = '1'  and U.uid = '$uid'")->field("U.id as id,U.uid as uid,U.eid as eid,yjequipment.e_name as e_name,yjequipment.remark as remark,yjequipment.logo as logo,yjequipment.description as description")->select();
		if(!$data1){
			$datas['define'] = array();
		}else{
			foreach ($data1 as $key=>$d){
				if($d['logo']){
					$image = D('attach')->where('attach_id='.$d['logo'])->find();
					$image_url = $image['save_path'].$image['save_name'];
					$image_url = getImageUrl($image_url);
					$data1[$key]['logo'] = $image_url;
				}
			}
			$datas['define'] = $data1;
		}
			$map['eid'] = array('not in','2');
			$map['status'] = 1;
			$data2 =  $equipment->where($map)->select();
			foreach ($data2 as $key=>$d){
				if($d['logo']){
					$image = D('attach')->where('attach_id='.$d['logo'])->find();
					$image_url = $image['save_path'].$image['save_name'];
					$image_url = getImageUrl($image_url);
					$data2[$key]['logo'] = $image_url;
				}
			}
			
			$datas['undefine'] = $data2;
		
		//unset($datas);
		if($datas){
			$value = ['code'=>'success','data'=>$datas];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{//获取失败
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		
	}
	public function getData(){//【获取用户对应设备的全部运动数据】
		$uid = $_POST['uid'];
		$id = $_POST['id'];
		$data = $this->getUserdata($uid, $id);
		if($data){
			$value = ['code'=>'success','data'=>$data];
			//dump($value);exit;
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		
		}
	}
	
	
	public function addData(){//【添加用户对应设备运动数据】
		//增加传参
		$type =isset($_POST["type"])?$_POST["type"]:"common"; //order  common
		$oid = isset($_POST["oid"])?$_POST["oid"]:0;
		$act_time = $_POST["act_time"];
		$yangwo = $_POST["yangwo"]?1:0;//是否为仰卧板
		$num = $_POST["num"]?$_POST["num"]:0;//是否传入仰卧次数
		//echo 'uid';exit;
		$id = $_POST['id'];
		
		$uid = $_POST['uid'];
		//$eid = $_POST['eid'];
		$time = time();
//		$data[$time]['cal'] = $cal = $_POST['cal'];
//		
//		if($yangwo){
//			$data[$time]['num'] = $num;
//		}else{
//			$data[$time]['ratio'] = $ratio = $_POST['ratio'];
//		}
		$equipment_user = M('Equipment_user');
		//echo $id;exit;
		$user = $equipment_user->where("id = $id")->find();
		if(!$user){//找不到该用户的设备记录
				$value = ['code'=>'undefine'];
				header("Content-Type: text/json");
				die(json_encode($value));
		
		}
		$result = $equipment_user->where("id = '$id' and uid = $uid")->field("data")->find();
		
		if($result['data']){//已经存在数据就叠加
			
			$result = json_decode($result['data'],true);
			if(count($result)>100){$key = array_keys($result);unset($result[$key[0]]);}//去掉第一个元素
			$result = array_values($result);//格式化数组键名，从0开始递增
			$count = count($result);//当前数据的个数
			$cal = $_POST['cal'];
			
			if($yangwo){
				$result[$count] = array('time'=>$time,'cal'=>$cal,'num'=>$num);
			}else{
				$ratio = $_POST['ratio'];
				$result[$count] = array('time'=>$time,'cal'=>$cal,'ratio'=>$ratio);
			}
			
			$str['data'] = json_encode($result);
			
			$save = $equipment_user->where("id = '$id' and uid = $uid")->data($str)->save();
			if($save){
				$this->addSportData($type, $act_time, $cal, time(), $uid);
				unset($data);
				$data = $this->getUserdata($uid, $id);
				$value = ['code'=>'success','data'=>$data];
				header("Content-Type: text/json");
				die(json_encode($value));
			}else{//写入失败
				$value = ['code'=>'false'];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
		}else{//没有数据就直接写入数据
			
			$data[0]['time'] = $time;
			$data[0]['cal'] = $cal = $_POST['cal'];
			if($yangwo){
				$data[0]['num'] = $num;
			}else{
				$data[0]['ratio'] = $ratio = $_POST['ratio'];
			}
			
			
			$str['data'] = json_encode($data);
			$save = $equipment_user->where("id = '$id' and uid = $uid")->data($str)->save();
			if($save){
				$this->addSportData($type, $act_time, $cal, time(), $uid);
				unset($data);
				$data = $this->getUserdata($uid, $id);
				$value = ['code'=>'success','data'=>$data];
				header("Content-Type: text/json");
				die(json_encode($value));
			}else{//写入失败
				$value = ['code'=>'false'];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
		}
	
	}
	
	public function getEquipemtinfo(){//【获取设备信息】
		$eid = $_POST['eid'];
		$equipment = M('Equipment');
		$data = $equipment->where("eid = $eid")->find();
		if($data){
			
				if($data['logo']){
					$image = M('attach')->where('attach_id='.$data['logo'])->find();
					
					$image_url = $image['save_path'].$image['save_name'];
					$image_url = getImageUrl($image_url);
					$data['logo'] = $image_url;
				}
		
			
				$value = ['code'=>'success','data'=>$data];
				//dump($value);exit;
				header("Content-Type: text/json");
				die(json_encode($value));
		}else{
				$value = ['code'=>'false'];
				//dump($value);exit;
				header("Content-Type: text/json");
				die(json_encode($value));
		}
	}
	
	public function addSportData($type,$act_time,$cal,$sport_time,$uid,$oid=0){
		
		$map['act_time'] =$act_time;
		$map['cal'] = $cal;
		$map['sport_time'] = $sport_time;
		
		//判断是约单中进行的还是普通进行
		if($type == "order"){

			//修改状态，添加运动数据
			if(M("Order_data")->where("oid = $oid and uid = $uid")->save($map)){
				//更新该约单的排名
				$this->updatePlace($oid);
			}
		}
		$today = strtotime(date("Ymd"))-1;
		//判断今天是否第一次运动
		$sport = M("User_sport_data")->where("uid = $uid and sport_time>$today")->find();
// 		dump($sport);
// 		dump(isset($sport));
// 		dump(mysql_affected_rows($sport));
// 		dump(isset($sport["id"]));
		if (!isset($sport["id"])){
			//是，增加信息
			$map['uid'] = $uid;
			if(M("User_sport_data")->add($map)){
			
			}else{
				$value = ['code'=>'wrong',"massage"=>"add sportdata wrong ".M("User_sport_data")->getDbError()];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
		}else{
			//否，修改信息
			$id = $sport["id"];
			$sp['sport_time']= $sport_time;
			$sp["cal"] = $cal+$sport["cal"];
			$sp["act_time"] = $act_time +$sport["act_time"];
			if(M("User_sport_data")->where("id = $id")->save($sp)){
				
			}else{
				$value = ['code'=>'wrong',"massage"=>"save wrong ".M("User_sport_data")->getDbError()];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
		}
	
	
	}
	
	
	
	
	
	
}